package com.wl.shop.web;

import java.beans.PropertyEditorSupport;
import java.util.Date;

import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;

import com.wl.shop.service.ArticleService;
import com.wl.shop.service.AttachmentService;
import com.wl.shop.service.CategoryService;
import com.wl.shop.service.DictService;
import com.wl.shop.service.FieldArticleService;
import com.wl.shop.service.FieldCategoryService;
import com.wl.shop.service.MenuService;
import com.wl.shop.service.RoleMenuService;
import com.wl.shop.service.RoleService;
import com.wl.shop.service.TemplateService;
import com.wl.shop.service.UserRoleService;
import com.wl.shop.service.UserService;
import com.wl.shop.utils.DateUtils;

public	abstract class BaseController {
	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	/**
	 * 管理基础路径
	 */
	@Value("${adminPath}")
	protected String adminPath;
	
	/**
	 * 前端基础路径
	 */
	@Value("${frontPath}")
	protected String frontPath;
	
	/**
	 * 参数绑定异常
	 */
	@ExceptionHandler({BindException.class, ConstraintViolationException.class, ValidationException.class})
    public String bindException() {  
        return "error/400";
    }
	
	/**
	 * 授权登录异常
	 */
	@ExceptionHandler({AuthenticationException.class})
    public String authenticationException() {  
        return "error/403";
    }
	
	/**
	 * 初始化数据绑定
	 * 1. 将所有传递进来的String进行HTML编码，防止XSS攻击
	 * 2. 将字段中Date类型转换为String类型
	 */
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		// String类型转换，将所有传递进来的String进行HTML编码，防止XSS攻击
		binder.registerCustomEditor(String.class, new PropertyEditorSupport() {
			@Override
			public void setAsText(String text) {
				setValue(text == null ? null : StringEscapeUtils.escapeHtml4(text.trim()));
			}
			@Override
			public String getAsText() {
				Object value = getValue();
				return value != null ? value.toString() : "";
			}
		});
		// Date 类型转换
		binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
			@Override
			public void setAsText(String text) {
				setValue(DateUtils.parse(text, "yyyy-MM-dd"));
			}
//			@Override
//			public String getAsText() {
//				Object value = getValue();
//				return value != null ? DateUtils.formatDateTime((Date)value) : "";
//			}
		});
	}
	
	
	/**
	 * System Service
	 */
	@Autowired
	protected MenuService menuService;
	
	@Autowired
	protected RoleService roleService;
	
	@Autowired
	protected RoleMenuService roleMenuService;
	
	@Autowired
	protected UserRoleService userRoleService;
	
	@Autowired
	protected UserService userService;
	
	/**
	 * CMS Service
	 */
	@Autowired
	protected CategoryService categoryService;
	
	@Autowired
	protected ArticleService articleService;
	
	@Autowired
	protected TemplateService templateService;
	
	@Autowired
	protected AttachmentService attachmentService;
	
	@Autowired
	protected DictService dictService;
	
	@Autowired
	protected FieldCategoryService  fieldCategoryService;
	
	@Autowired
	protected FieldArticleService fieldArticleService;
	
}
